<template>
  <Form ref="loginForm"
        :model="form"
        :rules="rules"
        @keydown.enter.native="handleSubmit">
    <FormItem prop="userName" >
      <Input v-model="form.userName"
             placeholder="请输入用户名">
      <span slot="prepend">
        <Icon :size="16"
              type="ios-person"></Icon>
      </span>
       
      </Input>
    </FormItem>
    <FormItem prop="password">
      <Input type="password"
             v-model="form.password"
             placeholder="请输入密码">
      <span slot="prepend">
        <Icon :size="14"
              type="md-lock"></Icon>
      </span>
      </Input>
    </FormItem>
 
     <FormItem prop="rememberMe">
       
        <Checkbox v-model="form.rememberMe">
            <span>记住账号密码</span>
        </Checkbox>
            
     </FormItem>
    <FormItem>
      <Button @click="handleSubmit"
              type="primary"
              long>登录</Button>
    </FormItem>
  </Form>
</template>
<script>

import { localSave,localRead, localRemove, base64Encode , base64Decode } from "@/libs/util";


export default {
  name: "LoginForm",
  props: {
    userNameRules: {
      type: Array,
      default: () => {
        return [{ required: true, message: "账号不能为空", trigger: "blur" }];
      }
    },
    passwordRules: {
      type: Array,
      default: () => {
        return [{ required: true, message: "密码不能为空", trigger: "blur" }];
      }
    }
  },
  data() {
   
    return {
      form: {
        userName: '',
        password: '',
        rememberMe: false
      }
    };
  },
  computed: {
    rules() {
      return {
        userName: this.userNameRules,
        password: this.passwordRules
      };
    }
  }, 
    mounted() {
        let rememberMe = localRead("rememberMe");
        let login_username= localRead("login_username");
        
        console.log('rem000:', rememberMe);
        console.log('login_username:', login_username);
       
        if (rememberMe  && login_username) {
            
            let password = localRead("password");

            this.form.userName = login_username;
            this.form.password = base64Decode(password);// base64解密;
            this.form.rememberMe = true;
        }
    },
  methods: {

    handleSubmit() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {

            /* ------ 账号密码的存储 ------ */
            if (this.form.rememberMe) {
                let password = base64Encode(this.form.password); // base64加密
                localSave("login_username", this.form.userName);
                localSave("password", password);
                localSave("rememberMe", this.form.rememberMe);
            } else {  //localRemove
                localRemove("login_username");
                localRemove("password");
                localRemove("rememberMe");
            }

            this.$emit("on-success-valid", {
                userName: this.form.userName,
                password: this.form.password
            });
        }
      });
    }
  }
};
</script>
